<template>
  <div>
    <el-table :data="randomslist"
              style="width: 100%">
      <el-table-column prop="id"
                       label="编号"
                       min-width="200">
      </el-table-column>

      <el-table-column label="题型"
                       min-width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.questionType === '1'">单选</span>
          <span v-else-if="scope.row.questionType === '2'">多选</span>
          <span v-else>简答</span>

        </template>
      </el-table-column>

      <el-table-column label="题目编号"
                       min-width="200">
        <template class="questionIDs"
                  slot-scope="scope">
          <p @click="qusetionspreview(scope.row,index)"
             v-for="(item,index) in scope.row.questionIDs"
             :key="index">{{item.number}}</p>
        </template>
      </el-table-column>

      <el-table-column prop="addTime"
                       label="录入时间"
                       min-width="100">
      </el-table-column>

      <el-table-column prop="totalSeconds"
                       label="答题时间"
                       min-width="150">
      </el-table-column>

      <el-table-column prop="accuracyRate"
                       label="正确率"
                       min-width="100">
      </el-table-column>

      <el-table-column prop="userName"
                       label="录入人"
                       min-width="100">
      </el-table-column>

      <el-table-column label="操作"
                       min-width="100">
        <template slot-scope="scope">
          <el-button type="danger"
                     @click="delerandoms(scope.row)"
                     icon="el-icon-delete"
                     circle>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination @size-change="handleSizeChange"
                   @current-change="handleCurrentChange"
                   :current-page="query.page"
                   :page-sizes="[10, 20, 50, 100]"
                   :page-size="query.pagesize"
                   layout="total, sizes, prev, pager, next, jumper"
                   :total="counts">
    </el-pagination>
    <!-- <questions-preview v-if="isshow"
                       :preContent="this.questionsid"></questions-preview> -->
    <el-dialog title="题目预览"
               :visible.sync="isTestShow"
               width="56%">
      <questions-preview :pre-id="questionsid"
                         v-if="isTestShow"></questions-preview>
      <span slot="footer">
        <el-button @click="isTestShow = false">关闭</el-button>
        <el-button type="primary">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { randoms } from '@/api/hmmm/questions.js'
import { removeRandoms } from '@/api/hmmm/questions.js'
import questionsPreview from './questions-preview.vue'
export default {
  name: 'RandomList',
  components: {
    questionsPreview
  },
  props: {
    search: {
      type: [Number, String, Object],
      default: true
    }
  },
  data() {
    return {
      randomslist: [],
      counts: 0,
      query: {
        pagesize: 10,
        page: 1,
        keyword: this.search
      },
      questionsid: '',
      isshow: false,
      isTestShow: false
    }
  },
  created() {
    this.getrandomslist()
  },
  methods: {
    async getrandomslist() {
      const { data: res } = await randoms(this.query)
      this.randomslist = res.items
      this.counts = res.counts
    },
    handleSizeChange(val) {
      this.query.pagesize = val
      this.getrandomslist()
    },
    handleCurrentChange(val) {
      this.query.page = val
      this.getrandomslist()
    },
    async delerandoms(val) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })

        .then(async () => {
          const { data: res } = await removeRandoms({
            id: val.id
          })
          this.getrandomslist()
          if (this.randomslist.length != 0) {
            this.query.page--
          }
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    searchrandomslist() {
      this.query.keyword = this.search
      this.getrandomslist()
    },
    qusetionspreview(val, index) {
      this.isTestShow = true

      this.questionsid = val.questionIDs[index].id
      console.log(val.questionIDs[index].id)
    }
  }
}
</script>

<style lang="less" scope>
p {
  color: #4f99ff;
}
</style>